/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package recanto.das.pedrinhas.dao;

import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import recanto.das.pedrinhas.bean.Cliente;
import util.NewHibernateUtil;

/**
 *
 * @author Guilherme
 */
public class ClienteDao {
    private Session session;
    
    
    public ClienteDao(){
        this.session = NewHibernateUtil.getSessionFactory().openSession();
    }
    
    public void salvar (Cliente cliente){
        try{
            Transaction transaction = session.beginTransaction();
            this.session.save(cliente);
            transaction.commit();
        }catch(HibernateException ex){
           System.out.print("Erro: "+ex); 
        }
    }
    
    public void salvarAcompanhante(Cliente cliente) {
        try{
            Transaction transaction = session.beginTransaction();
            this.session.save(cliente);
            transaction.commit();
        } catch (HibernateException ex) {
            System.out.println("Erro " + ex);
        }
    }
    
    public void alterar(Cliente cliente){
        try{
            Transaction transaction = session.beginTransaction();
            this.session.update(cliente);
            transaction.commit();
        }catch(HibernateException ex){
            System.out.print("Erro: "+ex); 
        }
    }
    
    public void deletar(Cliente cliente){
        try{
            this.session.delete(cliente);
        }catch(HibernateException ex){
            System.out.print("Erro: "+ex); 
        }
    }
    
    public List<Cliente> buscaPorNome(String nome) {
        try{
            return session.createCriteria(Cliente.class)
                .add(Restrictions.ilike("nome", nome, MatchMode.ANYWHERE))
                .list();
        }catch(HibernateException ex){
            System.out.println("Erro: "+ex);
            return null;
        }
    }
    
    public Cliente buscaPorCpf(String cpf){
        try{
            return (Cliente) session.createCriteria(Cliente.class)
                    .add(Restrictions.ilike("cpf", cpf, MatchMode.EXACT)).uniqueResult();
        }catch(HibernateException ex){
            System.out.print("Erro: "+ex); 
            return null;
        }
    }
    
    public List<Cliente> buscaPorData(Date data){
        Date hoje = new Date();
        try{
            return session.createCriteria(Cliente.class)
                    .add(Restrictions.ilike("datanasc", hoje))
                    .list();
        }catch(HibernateException ex){
            System.out.print("Erro: "+ex); 
            return null;
        }
    }
    
    public Cliente carregaPorId (int idCli){
        try{
            return(Cliente) this.session.load(Cliente.class, idCli);
        }catch(HibernateException ex){
            System.out.print("Erro: "+ex); 
            return null;
        }
    }
    
    public List<Cliente> carregaClientes (){
        try{
            return this.session.createCriteria(Cliente.class).list();
        }catch(HibernateException ex){
            System.out.println("Erro: "+ex);
            return null;
        }
    }
    
    public void fechaSession (){
        try{
            session.close();
        }catch(HibernateException ex){
            System.out.println("Erro: "+ex);
        }
    }
    
    
}
